﻿// ## 2023/05/17 # 江东新风 # 特技参数函数调整 ##
// ## 2023/5/03 # 铃 # 修改了难度的系数影响.并且降低了粮草收入(人口系统下粮草收入太高了,大约是金收入的30倍) ##
// ## 2023/04/20 # 江东新风 # 同步玲修改并修复小bug ##
// ## 2023/01/23 # 铃 # 根据城市的人口特征,再次优化算法 ##
// ## 2022/12/29 # 铃 # 再次优化人口系统,同时优化资金算法,使得收入更平滑 ##
// ## 2022/11/02 # 铃 # 修改了资金收入算法,以适配新的人口系统 ##
// ## 2022/03/27 # 江东新风 # 军屯农倍率线性变化 ##
// ## 2022/02/17 # 江东新风 # 探索涨基础粮收入 ##
// ## 2022/02/14 # 江东新风 # 部分常量中文化 ##
// ## 2021/10/29 # 江东新风 # 结构体存储调用方式改进 ##
// ## 2021/10/10 # 江东新风 # 人口相关设定 ##
// ## 2021/10/01 # 江东新风 # namespace的韩文改成英文 ##
// ## 2021/02/17 # 江东新风 # 城市数惩罚设置上限，调整系数 ##
// ## 2021/01/29 # 江东新风 # 修复张鲁势力产粮异常 ##
// ## 2020/12/12 # 江东新风 # 修复trace参数报错 ##
// ## 2020/10/23 #江东新风#同步马术书大神的更新，添加城市数量惩罚，修复战役模式情况的nullptr错误##
// ## 2020/10/07 # 氕氘氚 # 修复谷仓未建成就生效、军屯农无效果的bug ##
// ## 2020/09/28 # 氕氘氚 # 谷仓允许不相邻、谷仓对军屯农生效，玩家电脑分别设置倍率 ##
// ## 2020/09/05 # 氕氘氚 #变法bug修复##
// ## 2020/08/19 # 江东新风 #新特技-变法##
// ## 2020/07/26 ##
namespace CITY_HARVEST
{
	const bool 调试模式 = false;

	//---------------------------------------------------------------------------------------

	class Main
	{
		Main()
		{
			pk::set_func(151, pk::func151_t(callback));
		}

		int callback(pk::city @city)
		{
			if (city is null or !pk::is_valid_force_id(city.get_force_id()))
				return 0;

			pk::force @force = pk::get_force(city.get_force_id());
			// 城市基础收入
			int n = city.harvest;
			int population_yield = 0;
			int farm_yield = 0;
			int city_id = city.get_id();
			auto base_t = ch::get_base_p(city_id);
			bool 丰收状态 = city.housaku;

			float population = float(base_t.population + base_t.mil_pop_all);

			population_yield = inter::get_population_harvest(city_id);

			pk::int_int yield = inter::cal_food_yield(city);
			farm_yield = yield.second;
			int basic_yield = yield.first;

			float 农场效率 = inter::get_farm_efficiency(population, basic_yield);

			farm_yield = int(farm_yield * 农场效率);

			n = population_yield + farm_yield;

			// pk::trace(pk::format("{},population_yield:{},farm_yield:{},农场效率:{}", pk::decode(pk::get_name(city)), population_yield, farm_yield,农场效率));

			if (pk::get_kunshu_id(city) == 武将_张鲁)
				n = int(n * 1.25f);

			pk::building @building = pk::city_to_building(city);
			// 全局修正
			n = inter::incomes_global_impact(n, building, 1); // 铃神改版的difficulty effect

			n += ch::get_base_p(city.get_id()).harvest_bonus;

			if (金粮收入_玩家_城市数_惩罚 and city.is_player() and !pk::is_campaign())
			{
				
				float force_city_count = float(pk::get_city_list(force).count);
				n = int(n * (1.f - pk::min(0.3f, (force_city_count - 3) * 0.01f)));
			}

			int buff = 0;
			if (city_tech::is_force_has_city(force, 39))
				buff +=5;

			n = int(n * (100 + buff) / 100);

			int base_harvest = n;

			// 丰收状态和特技不叠加
			if (丰收状态)
			{
				n = n * 2;
			}
			else
			{
				// 下方函数用于获取城市收支，以便计算内政自动化各类花费---可以考虑是否有更好的写法
				pk::bool_bool skill_check = ch::has_skill(pk::city_to_building(city), 特技_米道, 特技_征收);
				bool 米道特技 = skill_check.first;
				bool 征收特技 = skill_check.second;

				if (米道特技 and !征收特技)
					base_harvest = int(base_harvest * 1.5);

				if (!米道特技 and 征收特技)
					base_harvest = int(base_harvest * 0.5 * 3);

				if (米道特技 and 征收特技)
					base_harvest = int(base_harvest * 0.5) + int(base_harvest * 0.5) + int(base_harvest * 0.5 * 1.5);
			}
			base_t.base_harvest = uint32(base_harvest);

			return n;
		}

	}

	Main main;

	int get_harvest(pk::city @city)
	{
		return main.callback(city);
	}

	int get_infact_harvest(pk::city @city)
	{
		main.callback(city);
		auto base_t = ch::get_base_p(city.get_id());
		return base_t.base_harvest;
	}
}